﻿#include "purchase_dialog.h"
#include "ui_purchase_dialog.h"
#include "basal_sql.h"
#include "QCompleter"
#include <QMessageBox>
#include <QDebug>
//进货界面
purchase_dialog::purchase_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::purchase_dialog)
{
    ui->setupUi(this);
    ui->editPid->setFocus();

    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QIntValidator *validator = new QIntValidator(this);
    ui->editGetprice->setValidator(validator);
    ui->editSellprice->setValidator(validator);


    QIntValidator *validator1 = new QIntValidator(1600, 1700, this);
    ui->editPid->setValidator(validator1);
    QIntValidator *validator2 = new QIntValidator(1, 1700, this);
    ui->lineEdit_4->setValidator(validator2);

    ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //取消水平滚动条
}

purchase_dialog::~purchase_dialog()
{
    delete ui;
}

//待修改
void purchase_dialog::on_pushButton_clicked()
{
    QSqlQuery query;
    QString pId = ui->editPid->text();
    QString pName = ui->editPname->text();
    QString pGet = ui->editGetprice->text();
    QString pSell = ui->editSellprice->text();
    int pNum = ui->lineEdit_4->text().toInt();
    int afterNum = 0;
    int beforeNum = 0;

    QString sql = "select p_num from produce where p_id='" +pId+ "'";
    query.exec(sql);
    if(query.next()){//有此商品
        beforeNum = query.value(0).toInt();
        afterNum = beforeNum + pNum;
        //qDebug()<<afterNum;
        query.exec(QString("update produce set p_num='" +QString::number(afterNum)
                           + "' where p_id='" +pId+ "'"));
        while(ui->tableWidget->rowCount()){
            ui->tableWidget->removeRow(0);
        }
        purchase_dialog::showEvent(NULL);
    }
    else if(pNum)  //因为程序上边限制了PID的输入范围为1600-1700要是输入的小于1600会出现bug所以在这做了限制
    {
       QString sql1 = "insert into produce values('" +pId+ "','" +pName+ "',"
                       "'" +pGet+ "','" +pSell+ "','" +QString::number(pNum)+ "') ";
       if(query.exec(sql1)){

          QTableWidgetItem *pIdItem = new QTableWidgetItem(pId);
          QTableWidgetItem *pNameItem = new QTableWidgetItem(pName);
          QTableWidgetItem *pGetPriceItem = new QTableWidgetItem(pGet);
          QTableWidgetItem *pSellPriceItem = new QTableWidgetItem(pSell);
          QTableWidgetItem *pNumItem = new QTableWidgetItem(QString::number(pNum));

          int i = ui->tableWidget->rowCount();
          ui->tableWidget->insertRow(i);

          ui->tableWidget->setItem(i, 0, pIdItem);
          ui->tableWidget->setItem(i, 1, pNameItem);
          ui->tableWidget->setItem(i, 2, pGetPriceItem);
          ui->tableWidget->setItem(i, 3, pSellPriceItem);
          ui->tableWidget->setItem(i, 4, pNumItem);
          QMessageBox::about(NULL, "提示", "添加成功");

       }
       else{
           QMessageBox::about(NULL, "提示", "添加失败");
       }
    }
}


void purchase_dialog::showEvent(QShowEvent *event)
{
    sqlite3 *pDb;
    if(sqlite3_open("./supermarket.db", &pDb)==SQLITE_OK){
        QString sql = "select * from produce order by p_id DESC;";
        sqlite3_stmt *pStmt;
        if(sqlite3_prepare(pDb,sql.toStdString().c_str(), sql.length(), &pStmt, NULL) == SQLITE_OK){
            while(sqlite3_step(pStmt) == SQLITE_ROW){
                const unsigned char *pId = sqlite3_column_text(pStmt, 0);
                const unsigned char *pName = sqlite3_column_text(pStmt, 1);
                const unsigned char *pGetPrice = sqlite3_column_text(pStmt, 2);
                const unsigned char *psellPrice = sqlite3_column_text(pStmt, 3);
                const unsigned char *pNum = sqlite3_column_text(pStmt, 4);

                QTableWidgetItem *pIdItem = new QTableWidgetItem((char *)pId);
                QTableWidgetItem *pNameItem = new QTableWidgetItem((char *)pName);
                QTableWidgetItem *pGetPriceItem = new QTableWidgetItem((char *)pGetPrice);
                QTableWidgetItem *psellPriceItem = new QTableWidgetItem((char *)psellPrice);
                QTableWidgetItem *pNumItem = new QTableWidgetItem((char *)pNum);

                ui->tableWidget->insertRow(0);

                ui->tableWidget->setItem(0, 0, pIdItem);
                ui->tableWidget->setItem(0, 1, pNameItem);
                ui->tableWidget->setItem(0, 2, pGetPriceItem);
                ui->tableWidget->setItem(0, 3, psellPriceItem);
                ui->tableWidget->setItem(0, 4, pNumItem);
            }
        }
    }
}

//输入商品编号回车查询数据库信息并显示
void purchase_dialog::on_editPid_returnPressed()
{
    QSqlQuery query;
    QString id = ui->editPid->text();
    query.exec(QString("select p_name, p_get_price, p_sell_price from produce where p_id='" +id+ "'"));
    if(query.next()){
        //禁止修改数据库中默认商品信息
        ui->editPname->setEnabled(false);
        ui->editGetprice->setEnabled(false);
        ui->editSellprice->setEnabled(false);
        ui->lineEdit_4->setFocus();

        QString pName = query.value(0).toString();
        QString getPrice = query.value(1).toString();
        QString sellPrice = query.value(2).toString();
        ui->editPname->setText(pName);
        ui->editGetprice->setText(getPrice);
        ui->editSellprice->setText(sellPrice);
    }else{
        QMessageBox::about(NULL, "提示", "仓库无此产品，请添加");
        ui->editPname->setEnabled(true);
        ui->editGetprice->setEnabled(true);
        ui->editSellprice->setEnabled(true);

        // ui->editPid->clear();
        ui->editPname->clear();
        ui->editGetprice->clear();
        ui->editSellprice->clear();
    }
}

